package com.example.demo;

public class UnionFind {
    int[] friends;
    UnionFind(int n){
        this.friends=new int[n];
        for(int i=0;i<n;i++){
            friends[i]=i;
        }
    }
    public void union(int i,int j){
        friends[find(i)]=find(j);
    }
    public int find(int i){
        if(friends[i]!=i){
            friends[i]=find(friends[i]);
        }
        return friends[i];
    }
}
